package com.glb.gilibuy.web.mall;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glb.gilibuy.entity.Product;
import com.glb.gilibuy.entity.ProductCategory;
import com.glb.gilibuy.entity.ProductExample;
import com.glb.gilibuy.util.BeanUtil;
import com.glb.gilibuy.util.MapGenerator;
import com.glb.gilibuy.util.UploadUtil;
import com.glb.gilibuy.web.BaseController;
import com.glb.gilibuy.web.vo.ProductCategoryVO;
import com.glb.gilibuy.web.vo.ProductDetailVO;
import com.glb.gilibuy.web.vo.SearchedProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/4 10:30
 */
@Api(tags = "商品模块")
@Controller
public class ProductController extends BaseController {
    @ApiOperation(value = "查询商品列表", notes = "查询商品列表", produces = "application/json")
    @GetMapping("/search")
    @ResponseBody
    public Map<String, Object> searchProducts(@RequestParam("keyword") String keyword,
                                              //排序关键词：time、price、heat、saleCount、(commentCount)
                                              @RequestParam(value = "sort", required = false) String sort,
                                              @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                              @RequestParam(value = "page", defaultValue = "1") Integer page) {
        if (StringUtils.isEmpty(keyword)) {
            return MapGenerator.getRes(500, "关键词参数为空！");
        }
        //特殊口令查全表
        if ("@admin@".equals(keyword)) {
            keyword = "";
        }
        //每页6个
        PageHelper.startPage(page, 6);
        List<Product> productList = productService.selectProductsByKeyword(keyword, productSort(sort, isASC), 0);
        PageInfo pageInfo = new PageInfo(productList);

        if (!productList.isEmpty()) {
            List<SearchedProductVO> productVOList = BeanUtil.copyList(productList, SearchedProductVO.class);
            pageInfo.setList(productVOList);

            Map<String, Object> data = new HashMap<>();
            data.put("keyword", keyword);
            data.put("pageInfo", pageInfo);
            return MapGenerator.getResWithData(200, "查询成功！", data);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    @ApiOperation(value = "获取商品分类列表", notes = "获取商品分类列表(单向分级)", produces = "application/json")
    @GetMapping("/categoryList")
    @ResponseBody
    public Map<String, Object> getCategories() {
        List<ProductCategoryVO> productCategoryVOList = productCategoryService.selectAllCategories();
        if (!productCategoryVOList.isEmpty()) {
            System.out.println(productCategoryVOList.toString());
            return MapGenerator.getResWithData(200, "获取商品分类列表成功！", productCategoryVOList);
        } else {
            return MapGenerator.getRes(201, "获取结果为空！");
        }

    }

    @ApiOperation(value = "根据分类获取商品列表", notes = "根据分类获取商品列表", produces = "application/json")
    @GetMapping("/productList")
    @ResponseBody
    public Map<String, Object> searchProductsByCategory(@RequestParam("categoryIds") String[] categoryIds,
                                                        @RequestParam(value = "sort", required = false) String sort,
                                                        @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page) {
        for (String categoryId : categoryIds) {
            ProductCategory productCategory = productCategoryService.selectCategoryByCategoryId(categoryId);
            if (StringUtils.isEmpty(productCategory)) {
                return MapGenerator.getRes(500, "含有不存在的分类！");
            }
        }
        //每页6个
        PageHelper.startPage(page, 6);
        PageInfo pageInfo = productService.findProductsByCategoryIds(categoryIds, productSort(sort, isASC), 0);

        if (pageInfo != null) {
            return MapGenerator.getResWithData(200, "查询成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "获取结果为空！");
        }
    }

    @ApiOperation(value = "获取单个商品详情", notes = "获取单个商品详情（暂定）", produces = "application/json")
    @GetMapping("/detail")
    @ResponseBody
    public Map<String, Object> getProductDetail(@RequestParam("productId") String productId) {

        Product product = productService.selectProductByProductId(productId);
        if (StringUtils.isEmpty(product)) {
            return MapGenerator.getRes(500, "不存在此商品！");
        }

        ProductDetailVO productVO = new ProductDetailVO();
        BeanUtil.copyOne(product, productVO);

        productVO.setPicList(UploadUtil.stringToList(product.getPics()));

//        ProductCategory productCategory = productCategoryServicce.selectCategoryByCategoryId(product.getProductCategoryId());
//        productVO.setProductCategory(productCategory.getProductCategoryName());

//        getComments
//        合成map作为data

        return MapGenerator.getResWithData(200, "获取商品详情成功！", productVO);

    }

    @ApiOperation(value = "获取轮播图列表", notes = "获取轮播图列表", produces = "application/json")
    @GetMapping("/cyclePicList")
    @ResponseBody
    public Map<String, Object> getCyclePicList(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        //每页6个
        PageHelper.startPage(page, 6);
        PageInfo pageInfo = cyclePicService.findAllCyclePics();
        if (pageInfo != null) {
            return MapGenerator.getResWithData(200, "获取轮播图列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "获取结果为空！");
        }
    }

    //排序参数转化
    private ProductExample productSort(String sort, boolean isASC) {
        ProductExample productExample = new ProductExample();
        sort = sort == null ? "" : sort;
        String column = "";
        String order = "desc";
        if (isASC) {
            order = "asc";
        }
        switch (sort) {
            case "time":
                column = "gmt_create";
                break;
            case "heat":
                column = "product_heat";
                break;
            case "saleCount":
                column = "sale_count";
                break;
            case "commentCount":
                column = "comment_count";
                break;
            case "price":
                column = "product_price";
                break;
            default:
        }
        if ("".equals(column)) {
            productExample.setOrderByClause("product_sequence desc , id desc");
        } else {
            productExample.setOrderByClause(String.format("%s %s , product_sequence desc , id desc", column, order));
        }
        return productExample;
    }

}
